﻿using CQIE.LabAssist.DataAccesss;
using CQIE.LabAssist.Models;
using CQIE.LabAssist.PublicHelp;
using Microsoft.AspNetCore.Connections;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using RabbitMQ.Client;
using System.Text;

namespace CQIE.LabAssist.Controllers
{
    /// <summary>
    /// 检查控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class CheckController : Controller
    {
        private readonly ICheckService _checkService;

        //public CheckController(ICheckService CheckService)
        //{
        //    _checkService = CheckService;
        //}
        #region 获取所有检查记录

        /// <summary>
        /// 获取所有检查记录
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public JsonResult getAllCheck()
        {
            return new JsonResult(CheckService.Instance.getAllCheck());
        }
        #endregion

        /// <summary>
        /// 通过实验室id获取
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public JsonResult getCheckByLabId(int id)
        {
            return new JsonResult(CheckService.Instance.getCheckByLabId(id));
        }

        /// <summary>
        /// 通过实验员id获取
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public JsonResult getCheckByUserId(int id)
        {
            return new JsonResult(CheckService.Instance.getCheckByUserId(id));
        }


        #region 新增检查记录
        public record check
        {
            public bool Doors_windows { get; set; }
            /// <summary>
            /// 门窗状态备注，描述门窗的具体情况
            /// </summary>
            public string Doors_windows_state { get; set; }
            /// <summary>
            /// 电器线路状态，表示电器线路是否正常
            /// </summary>
            public bool Electrical_wiring { get; set; }
            /// <summary>
            /// 电器线路备注，描述电器线路的具体情况
            /// </summary>
            public string Electrical_wiring_state { get; set; }
            /// <summary>
            /// 消防器件有无，表示消防器件是否存在
            /// </summary>
            public bool Firefighting_facilities { get; set; }
            /// <summary>
            /// 消防器件有无备注，描述消防器件的具体情况
            /// </summary>
            public string Firefighting_facilities_state { get; set; }

            /// <summary>
            /// 消防器件有效期，表示消防器件是否在有效期内
            /// </summary>
            public bool Firefighting_facilities_validity { get; set; }

            /// <summary>
            /// 消防器件有效期备注，描述消防器件有效期的具体情况
            /// </summary>
            public string Firefighting_facilities_validity_state { get; set; }

            /// <summary>
            /// 仪器设备能否使用，表示仪器设备是否可正常使用
            /// </summary>
            public bool Instruments_equipment { get; set; }

            /// <summary>
            /// 仪器设备能否使用备注，描述仪器设备的具体情况
            /// </summary>
            public string Instruments_equipment_state { get; set; }

            /// <summary>
            /// 防盗设施是否完好，表示防盗设施是否完好无损
            /// </summary>
            public bool Anti_theft_facilities { get; set; }

            /// <summary>
            /// 防盗设施是否完好备注，描述防盗设施的具体情况
            /// </summary>
            public string Anti_theft_facilities_state { get; set; }

            /// <summary>
            /// 警示标志，表示是否有警示标志
            /// </summary>
            public bool Warning_sign { get; set; }

            /// <summary>
            /// 警示标志备注，描述警示标志的具体情况
            /// </summary>
            public string Warning_sign_state { get; set; }

            /// <summary>
            /// 设备仪器其他安全隐患，表示设备仪器是否存在其他安全隐患
            /// </summary>
            public bool IEOther_safety_hazards { get; set; }

            /// <summary>
            /// 设备仪器其他安全隐患备注，描述设备仪器的其他安全隐患
            /// </summary>
            public string IEOther_safety_hazards_state { get; set; }

            /// <summary>
            /// 物品及清洁，表示物品的摆放和清洁情况是否良好
            /// </summary>
            public bool Item_hygiene { get; set; }

            /// <summary>
            /// 物品及清洁备注，描述物品摆放和清洁的具体情况
            /// </summary>
            public string Item_hygiene_state { get; set; }

            /// <summary>
            /// 其他安全隐患，表示是否存在其他安全隐患
            /// </summary>
            public bool Other_safety_hazards { get; set; }

            /// <summary>
            /// 其他安全隐患备注，描述其他安全隐患的具体情况
            /// </summary>
            public string Other_safety_hazards_state { get; set; }
           
            /// <summary>
            /// 所属学期ID
            /// </summary>
            public int semesterId { get; set; }

            /// <summary>
            /// 实验室ID
            /// </summary>
            public List<int> laboratoryIds { get; set; }
        }
        // 建立队列工厂
        RabbitMQ.Client.IConnectionFactory factory = new ConnectionFactory
            {
                HostName = "127.0.0.1", // IP地址
                Port = 5672, // 端口号
                UserName = "guest", // 用户账号
                Password = "guest" // 用户密码
            };
        /// <summary>
        /// 新增检查记录
        /// </summary>
        /// <param name="check"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult addCheck([FromBody] check check)
        {


            

            // 创建连接对象和连接会话对象
            using (var connection = factory.CreateConnection())
            using (var channel = connection.CreateModel())
            {
                // 项目A发送消息队列
                channel.ExchangeDeclare(exchange: "public-exchange", type: "direct");

                // 定义队列
                string queueName = "login-queue";
                channel.QueueDeclare(queueName, false, false, false, null);

                // 发送消息到交换器
                var severity = "laboratory-queue"; // 路由键
                string message = JsonConvert.SerializeObject(check); // 队列中的消息
                var body = Encoding.UTF8.GetBytes(message);

                // 交换器与队列绑定
                channel.QueueBind(queueName, "public-exchange", severity, null);

                // 发送到队列
                var properties = channel.CreateBasicProperties();
                properties.Persistent = true;

                channel.BasicPublish(exchange: "public-exchange",
                                     routingKey: severity,
                                     basicProperties: properties,
                                     body: body);

                Console.WriteLine(" [x] Sent '{0}':'{1}'", severity, message);
            }

            return new JsonResult("");
            

            //List<int> checkRecodHelps = new List<int>();
           
            //Check newcheck = new Check
            //{           
            //    Anti_theft_facilities = check.Anti_theft_facilities,
            //    Anti_theft_facilities_state = check.Anti_theft_facilities_state,
            //    Doors_windows = check.Doors_windows,
            //    Doors_windows_state = check.Doors_windows_state,
            //    Electrical_wiring = check.Electrical_wiring,
            //    Electrical_wiring_state = check.Electrical_wiring_state,
            //    Firefighting_facilities = check.Firefighting_facilities,
            //    Firefighting_facilities_state = check.Firefighting_facilities_state,
            //    Firefighting_facilities_validity = check.Firefighting_facilities_validity,
            //    Firefighting_facilities_validity_state = check.Firefighting_facilities_validity_state,
            //    Instruments_equipment = check.Instruments_equipment,
            //    Instruments_equipment_state = check.Instruments_equipment_state,
            //    Warning_sign = check.Warning_sign,
            //    Warning_sign_state = check.Warning_sign_state,
            //    IEOther_safety_hazards = check.IEOther_safety_hazards,
            //    IEOther_safety_hazards_state = check.IEOther_safety_hazards_state,
            //    Item_hygiene = check.Item_hygiene,
            //    Item_hygiene_state = check.Item_hygiene_state,
            //    Other_safety_hazards = check.Other_safety_hazards,
            //    Other_safety_hazards_state = check.Other_safety_hazards_state,
            //    semesterId = check.semesterId
            //};
            //for (int i = 0; i < check.laboratoryIds.Count; i++)
            //{
            //    newcheck.laboratoryId = check.laboratoryIds[i];
                
            //    try
            //    {
            //        //checkRecodHelp.Success = _checkService.addCheck(newcheck);
            //        _checkService.addCheck(newcheck);
            //    }
            //    catch (Exception ex)
            //    {
            //        //checkRecodHelp.Success = false;
            //        //checkRecodHelp.reason = ex.ToString();
                  
            //        checkRecodHelps.Add(check.laboratoryIds[i]);
            //    }
                
            //}

            //return new JsonResult(checkRecodHelps);
        }
        #endregion

        #region 删除检查记录


        /// <summary>
        /// 删除检查记录
        /// </summary>
        /// <param name="checkId"></param>
        /// <returns></returns>
        [HttpDelete]
        public JsonResult deteleCheck(int checkId)
        {
            if (CheckService.Instance.deteleCheck(checkId))
            {
                return new JsonResult("删除成功");
            }
            else
            {
                return new JsonResult("删除失败");

            }

        }
        #endregion


        #region 修改检查记录
        public record upcheck
        {
            public int Id { get; set; }
            public bool Doors_windows { get; set; }
            /// <summary>
            /// 门窗状态备注，描述门窗的具体情况
            /// </summary>
            public string Doors_windows_state { get; set; }
            /// <summary>
            /// 电器线路状态，表示电器线路是否正常
            /// </summary>
            public bool Electrical_wiring { get; set; }
            /// <summary>
            /// 电器线路备注，描述电器线路的具体情况
            /// </summary>
            public string Electrical_wiring_state { get; set; }
            /// <summary>
            /// 消防器件有无，表示消防器件是否存在
            /// </summary>
            public bool Firefighting_facilities { get; set; }
            /// <summary>
            /// 消防器件有无备注，描述消防器件的具体情况
            /// </summary>
            public string Firefighting_facilities_state { get; set; }

            /// <summary>
            /// 消防器件有效期，表示消防器件是否在有效期内
            /// </summary>
            public bool Firefighting_facilities_validity { get; set; }

            /// <summary>
            /// 消防器件有效期备注，描述消防器件有效期的具体情况
            /// </summary>
            public string Firefighting_facilities_validity_state { get; set; }

            /// <summary>
            /// 仪器设备能否使用，表示仪器设备是否可正常使用
            /// </summary>
            public bool Instruments_equipment { get; set; }

            /// <summary>
            /// 仪器设备能否使用备注，描述仪器设备的具体情况
            /// </summary>
            public string Instruments_equipment_state { get; set; }

            /// <summary>
            /// 防盗设施是否完好，表示防盗设施是否完好无损
            /// </summary>
            public bool Anti_theft_facilities { get; set; }

            /// <summary>
            /// 防盗设施是否完好备注，描述防盗设施的具体情况
            /// </summary>
            public string Anti_theft_facilities_state { get; set; }

            /// <summary>
            /// 警示标志，表示是否有警示标志
            /// </summary>
            public bool Warning_sign { get; set; }

            /// <summary>
            /// 警示标志备注，描述警示标志的具体情况
            /// </summary>
            public string Warning_sign_state { get; set; }

            /// <summary>
            /// 设备仪器其他安全隐患，表示设备仪器是否存在其他安全隐患
            /// </summary>
            public bool IEOther_safety_hazards { get; set; }

            /// <summary>
            /// 设备仪器其他安全隐患备注，描述设备仪器的其他安全隐患
            /// </summary>
            public string IEOther_safety_hazards_state { get; set; }

            /// <summary>
            /// 物品及清洁，表示物品的摆放和清洁情况是否良好
            /// </summary>
            public bool Item_hygiene { get; set; }

            /// <summary>
            /// 物品及清洁备注，描述物品摆放和清洁的具体情况
            /// </summary>
            public string Item_hygiene_state { get; set; }

            /// <summary>
            /// 其他安全隐患，表示是否存在其他安全隐患
            /// </summary>
            public bool Other_safety_hazards { get; set; }

            /// <summary>
            /// 其他安全隐患备注，描述其他安全隐患的具体情况
            /// </summary>
            public string Other_safety_hazards_state { get; set; }

            /// <summary>
            /// 所属学期ID
            /// </summary>
            public int semesterId { get; set; }

            /// <summary>
            /// 实验室ID
            /// </summary>
            public int laboratoryId { get; set; }
        }
        /// <summary>
        /// 修改检查记录
        /// </summary>
        /// <param name="check"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult updateCheck([FromBody] upcheck check)
        {
            Check newcheck = new Check
            {
                Id = check.Id,
                Anti_theft_facilities = check.Anti_theft_facilities,
                Anti_theft_facilities_state = check.Anti_theft_facilities_state,
                Doors_windows = check.Doors_windows,
                Doors_windows_state = check.Doors_windows_state,
                Electrical_wiring = check.Electrical_wiring,
                Electrical_wiring_state = check.Electrical_wiring_state,
                Firefighting_facilities = check.Firefighting_facilities,
                Firefighting_facilities_state = check.Firefighting_facilities_state,
                Firefighting_facilities_validity = check.Firefighting_facilities_validity,
                Firefighting_facilities_validity_state = check.Firefighting_facilities_validity_state,
                Instruments_equipment = check.Instruments_equipment,
                Instruments_equipment_state = check.Instruments_equipment_state,
                Warning_sign = check.Warning_sign,
                Warning_sign_state = check.Warning_sign_state,
                IEOther_safety_hazards = check.IEOther_safety_hazards,
                IEOther_safety_hazards_state = check.IEOther_safety_hazards_state,
                Item_hygiene = check.Item_hygiene,
                Item_hygiene_state = check.Item_hygiene_state,
                Other_safety_hazards = check.Other_safety_hazards,
                Other_safety_hazards_state = check.Other_safety_hazards_state,
                laboratoryId = check.laboratoryId,
                semesterId = check.semesterId
            };

            if (CheckService.Instance.updateCheck(newcheck))
            {
                return new JsonResult("修改成功");
            }
            else
            {
                return new JsonResult("修改失败");
            }
        }

        #endregion

    }
}
